package com.iot08.admin.service;

import com.alibaba.fastjson.JSONObject;
import com.iot08.admin.mob.MyPushWorkBuilder;
import com.iot08.admin.mob.StatsV3ResVO;
import com.iot08.common.constant.ConstPush;
import com.iot08.common.entity.admin.MobNotify;
import com.iot08.common.entity.admin.MobNotifyPush;
import com.iot08.common.utils.StringUtil;
import lombok.extern.slf4j.Slf4j;
import mob.push.api.client.push.PushV3Client;
import mob.push.api.client.stats.StatsV3Client;
import mob.push.api.http.Http;
import mob.push.api.http.Result;
import mob.push.api.res.CancelWorkRes;
import mob.push.api.res.PushV3Res;
import mob.push.api.res.StatsV3Res;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * @version 1.0
 * @Description:
 * @time: 2020/12/29 10:49
 * @author: Rock
 */
@Slf4j
@Service
public class MobSendService {

    @Value("${mob.android.add_scheme}")
    public String MOB_COMMON_SCHEME;
    @Value("${mob.iOS.scheme}")
    public String MOB_IOS_COMMON_SCHEME;
    /**
     * @param notifyPush
     * @param notify
     * @param openids
     * @time: 2020/12/29 16:19
     * @author: Rock
     * @version 1.0
     */
    public String sendPush(MobNotifyPush notifyPush, MobNotify notify, Set<String> openids) {
        JSONObject message = new JSONObject();
        message.put("type", "systemUpdate");
        message.put("notifyId", notify.getId());
        message.put("msgId", notifyPush.getId());
        JSONObject data = new JSONObject();
        data.put("type", "accountMessage");
        data.put("message", message);

        Result<PushV3Res> resResult = null;
        if (notifyPush.getTarget().equals(MyPushWorkBuilder.TARGET_ALL)) {
            //广播
            resResult = PushV3Client.pushTaskV3(new MyPushWorkBuilder()
                    .setTargetAll(notifyPush, notify.getTitle(), notify.getContent(), MOB_COMMON_SCHEME, data.toJSONString())
                    .build());

        } else if (notifyPush.getTarget().equals(MyPushWorkBuilder.TARGET_ALIAS)) {
            resResult = PushV3Client.pushTaskV3(new MyPushWorkBuilder()
                    .setTargetByAlias(notifyPush, notify.getTitle(), notify.getContent(), MOB_COMMON_SCHEME, data.toJSONString(), openids)
                    .build());
        }
        log.info("resResult = {}", resResult);
        if (resResult != null && resResult.success()) {
            return resResult.getRes().getBatchId();
        }
        return null;
    }

    /**
     * @param workId
     * @time: 2020/12/30 14:46
     * @author: Rock
     * @version 1.0
     */
    public boolean sendRecall(String workId) {
        if (StringUtil.isNotBlank(workId)) {
            //发起撤回
            Result<CancelWorkRes> res = PushV3Client.recallPushTask(workId);
            log.info("sendRecall = {}", res);
            if (res != null && res.success()) {
                return true;
            }
        }
        return false;
    }

    /**
     * @param workId
     * @time: 2020/12/30 15:57
     * @author: Rock
     * @version 1.0
     */
    public StatsV3ResVO sendDetial(String workId) {
        if (StringUtil.isNotBlank(workId)) {
            Map<String, Object> params = new HashMap();
            params.put("workId", workId);
            StatsV3ResVO statsV3Res = new StatsV3ResVO();
            Result<StatsV3ResVO> res = Http.getResult(params, statsV3Res, "/v3/stats/getByWorkId");
            log.info("sendDetial = {}", res);
            if (res != null && res.success()) {
                return res.getRes();
            }
        }
        return null;
    }

    /**
     * @param workId
     * @time: 2020/12/31 9:20
     * @author: Rock
     * @version 1.0
     */
    public boolean sendCancel(String workId) {
        if (StringUtil.isNotBlank(workId)) {
            //发起取消
            Result<CancelWorkRes> res = PushV3Client.cancelPushTask(workId);
            log.info("sendCancel = {}", res);
            if (res != null && res.success()) {
                return true;
            }
        }
        return false;
    }

}
